package com.soft.app.ruanba;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import com.soft.app.ruanba.down.FileDownloadHandler;
import com.soft.app.ruanba.down.FileDownloadHelper;
import com.soft.app.ruanba.down.FileVo;


public class FileDownloadJob {
	private static final Logger log = LoggerFactory.getLogger(FileDownloadJob.class);
	private String downloadFileDir;
	private String xmlFile;
	public FileDownloadJob(){
		
	}
	
	public FileDownloadJob( String downloadFileDir, String xmlFile ){
		this.downloadFileDir = downloadFileDir;
		this.xmlFile = xmlFile;
	}
	
	public void excute(){
		SAXParserFactory saxfac = SAXParserFactory.newInstance();   
		try {   
			SAXParser saxparser = saxfac.newSAXParser();   
			InputStream is = new FileInputStream(xmlFile);
			FileDownloadHandler fdh = new FileDownloadHandler();
			saxparser.parse(is, fdh);   
			List<FileVo> fileVoes = fdh.getFiles();
			for( FileVo vo : fileVoes ){
				System.out.println(vo.getFileName());
				System.out.println(vo.getFileType());
				System.out.println(vo.getUrl());
				if(vo.getFileType().length() >= 3){
					FileDownloadHelper.download(downloadFileDir, vo.getFileName(), vo.getFileType(), vo.getUrl());
				}
			}
		} catch (ParserConfigurationException e) {   
			e.printStackTrace();   
		} catch (SAXException e) {   
			e.printStackTrace();   
		} catch (FileNotFoundException e) {   
			e.printStackTrace();   
		} catch (IOException e) {   
			e.printStackTrace();   
		}   
	}
	
	public static void main( String[] args ){
		String destFileDir = "";
		String xmlFile = "E:\\ruan8\\nodown\\ruan8_cate_11.xml";
		FileDownloadJob job = new FileDownloadJob(destFileDir, xmlFile);
		job.excute();
		
	}
}
